package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;

    @Override
    public void updateVisitNum(int userId, int num) {
        travelMapper.updateVisitNum(userId,num);
    }

    @Override
    public void updateWatchNum(int userId, int num) {
        travelMapper.updateWatchNum(userId,num);
    }

    /**
     * 根据关键字查询对应的游记条数
     * @param qo
     * @return
     */
    @Override
    public int selectCountByKeyword(TravelQueryObject qo) {
        return travelMapper.selectCountByKeyword(qo);
    }

    @Override
    public TravelContent selectContentById(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);


    }

    @Override
    public PageInfo selectTravelById(UserQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),true,false,null);//禁用合理化分页
        List<Travel> list = travelMapper.selectTravelById(qo.getId());
        return new PageInfo(list);
    }



    @Override
    public void changeState(Travel travel) {
        //如果是发布状态 设置发布时间
        if (travel.getState()== Travel.STATE_RELEASE) {
            travel.setReleaseTime(new Date());
        }
        //修改
        travelMapper.updateState(travel);
    }

    @Override
    public PageInfo<Travel> query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public Travel selectById(Long id) {
        return travelMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        if (travel.getId() != null) {
            //获取游记内容对象
            TravelContent travelContent = travel.getTravelContent();
            //主键关联
            travelContent.setId(travel.getId());
            //保存游记内容
            travelContentMapper.updateByPrimaryKey(travelContent);
            //更新
            travelMapper.updateByPrimaryKey(travel);
        }else{
            //设置作者
            travel.setAuthor(UserContext.getCurrentUser());
            //保存基本信息
            travelMapper.insert(travel);
            //获取游记内容对象
            TravelContent travelContent = travel.getTravelContent();
            //主键关联
            travelContent.setId(travel.getId());
            //保存游记内容
            travelContentMapper.insert(travelContent);
        }


    }

    @Override
    public PageInfo queryTravel(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }
}
